package com.sig.load;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

import com.sig.models.Point;
import com.sig.models.Segment;

public class LoadRoads extends DefaultHandler implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String BALISE_ROADS="roads";
	public static final String BALISE_ROAD="road";
	public static final String BALISE_SEGMENT="segment";
	public static final String BALISE_POINT1="point1";
	public static final String BALISE_POINT2="point2";
	public static final String ATTRIBUTE_X="x";
	public static final String ATTRIBUTE_Y="y";
	
	public static final String TAG="TAG";
	
	private List<Segment> listeSegment;
	private List<List<Segment>> listeRoads=new ArrayList<List<Segment>>();
	private Segment segment;
	private Point p1,p2;
	
	
	
	
	public LoadRoads(File infile) {
		super();
		this.load(infile, this);
	}


	public void startElement(String uri, String localName,
			String qName, Attributes attributes) throws SAXException{
		if(qName.equals(BALISE_ROADS)){
			Log.d(TAG, "roads");
			
		}
		else if(qName.equalsIgnoreCase(BALISE_ROAD)){
			Log.d(TAG, "road");
			listeSegment=new ArrayList<Segment>();
		}
		else if(qName.equals(BALISE_SEGMENT)){
			Log.d(TAG, "create new Segment");
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT1)){
			Log.d(TAG, "point");			
			p1=new Point(Float.parseFloat(attributes.getValue(ATTRIBUTE_X)),
						Float.parseFloat(attributes.getValue(ATTRIBUTE_Y)));
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT2)){
			Log.d(TAG, "point");			
			p2=new Point(Float.parseFloat(attributes.getValue(ATTRIBUTE_X)),
						Float.parseFloat(attributes.getValue(ATTRIBUTE_Y)));
		}
		
	}
	
	
	public void endElement(String uri, String localName, String qName)
			throws SAXException{
		if(qName.equals(BALISE_ROADS)){
		}
		else if(qName.equalsIgnoreCase(BALISE_ROAD)){				
			listeRoads.add(listeSegment);			//add list triangle in listpolygons		
		}
		else if(qName.equalsIgnoreCase(BALISE_SEGMENT)){						
			segment=new Segment(p1, p2);			//create new triangle and add into list
			listeSegment.add(segment);
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT1)){
			
		}
		else if(qName.equalsIgnoreCase(BALISE_POINT2)){
			
		}
		
	}
	
	
	/**
	 * Load the file 
	 */
	public void load(File in,LoadRoads roads) {
		SAXParserFactory saxFactory=SAXParserFactory.newInstance();
		try {
			SAXParser parser=saxFactory.newSAXParser();			
			parser.parse(in, roads);
			Log.d("FILE", "Read XML");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("FILE", " NOT found");
			e.printStackTrace();
		}
		
	}


	/**
	 * @return the listeRoads
	 */
	public List<List<Segment>> getListeRoads() {
		return listeRoads;
	}
	
	
	
}
